package com.zqq.goods.ui.activity

import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import cn.bingoogolapple.refreshlayout.BGANormalRefreshViewHolder
import cn.bingoogolapple.refreshlayout.BGARefreshLayout
import cn.bingoogolapple.refreshlayout.BGARefreshViewHolder
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.kennyc.view.MultiStateView
import com.zqq.baselibrary.ext.startLoading
import com.zqq.baselibrary.ui.activity.BaseMvpActivity
import com.zqq.baselibrary.ui.adapter.BaseRecyclerViewAdapter
import com.zqq.goods.R
import com.zqq.goods.injection.component.DaggerGoodsComponent
import com.zqq.goods.injection.module.GoodsModule
import com.zqq.goods.presenter.GoodsListPresenter
import com.zqq.goods.presenter.view.GoodsListView
import com.zqq.goods.service.bean.Goods
import com.zqq.goods.ui.adapter.GoodsAdapter
import com.zqq.provider.router.RouterPath
import kotlinx.android.synthetic.main.activity_goods.*

/**
 * 商品列表Activity（上拉刷新-下拉加载）
 * wc
 */
@Route(path = RouterPath.GoodsCenter.PATH_GOODS)
class GoodsActivity : BaseMvpActivity<GoodsListPresenter>(), GoodsListView, BGARefreshLayout.BGARefreshLayoutDelegate {

    private lateinit var mGoodsAdapter: GoodsAdapter
    private var mCurrentPage: Int = 1  //当前页
    private var mMaxPage: Int = 1       //最大页


    override fun injectComponent() {
        DaggerGoodsComponent.builder()
                .activityComponent(mActivityComponent)
                .goodsModule(GoodsModule())
                .build()
                .inject(this)
        mPresenter.mView = this
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_goods)
        initView()
        initRefreshLayout()
        loadData()
    }

    //初始化视图
    private fun initView() {
        mGoodsRv.layoutManager = GridLayoutManager(this, 2)
        mGoodsAdapter = GoodsAdapter(this)
        mGoodsRv.adapter = mGoodsAdapter

        //跳转到详情页
        mGoodsAdapter.setOnItemClickListener(object : BaseRecyclerViewAdapter.OnItemClickListener<Goods> {
            override fun onItemClick(item: Goods, position: Int) {
                ARouter.getInstance()
                        .build(RouterPath.GoodsCenter.PATH_GOODS_DETAIL)
                        .withInt(RouterPath.GoodsCenter.KEY_GOODS_ID, item.id)
                        .navigation()
            }
        })
    }

    //初始化刷新视图
    private fun initRefreshLayout() {
        mRefreshLayout.setDelegate(this)    //实例化本代理
        val viewHolder = BGANormalRefreshViewHolder(this, true)
        //加载时背景
        viewHolder.setLoadMoreBackgroundColorRes(R.color.common_bg)
        //刷新时背景
        viewHolder.setRefreshViewBackgroundColorRes(R.color.common_bg)
        mRefreshLayout.setRefreshViewHolder(viewHolder)
    }

    //加载数据
    private fun loadData() {
        if (intent.getIntExtra(RouterPath.GoodsCenter.KEY_SEARCH_GOODS_TYPE, 0) != 0) {
            mMultiStateView.startLoading()

            var searchKey = intent.getStringExtra(RouterPath.GoodsCenter.KEY_GOODS_KEYWORD)
            mPresenter.getGoodsListByKeyword(searchKey, mCurrentPage)
        } else {
            mMultiStateView.startLoading()
            var searchId = intent.getIntExtra(RouterPath.GoodsCenter.KEY_CATEGORY_ID, 1)
            mPresenter.getGoodsList(searchId, mCurrentPage)
        }
    }


    /*----------接口返回-------------*/
    //获取列表后回调
    override fun onGetGoodsListResult(result: MutableList<Goods>?) {
        mRefreshLayout.endLoadingMore()
        mRefreshLayout.endRefreshing()
        if (result != null && result.size > 0) {
            mMaxPage = result[0].maxPage
            if (mCurrentPage == 1) {
                mGoodsAdapter.setData(result)
            } else {
                mGoodsAdapter.dataList.addAll(result)
                mGoodsAdapter.notifyDataSetChanged()
            }
            mMultiStateView.viewState = MultiStateView.VIEW_STATE_CONTENT
        } else {
            mMultiStateView.viewState = MultiStateView.VIEW_STATE_EMPTY
        }
    }

    //上拉加载
    override fun onBGARefreshLayoutBeginLoadingMore(refreshLayout: BGARefreshLayout?): Boolean {
        return if (mCurrentPage < mMaxPage) {
            mCurrentPage++
            loadData()
            true
        } else {
            false
        }
    }

    //下拉刷新
    override fun onBGARefreshLayoutBeginRefreshing(refreshLayout: BGARefreshLayout?) {
        mCurrentPage = 1
        loadData()
    }

}
